package com.example.chatrecommendation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatrecommendation.bean.dto.AgentAddDto;
import com.example.chatrecommendation.bean.entity.Agent;
import com.example.chatrecommendation.service.AgentService;
import com.example.chatrecommendation.mapper.AgentMapper;
import com.example.chatrecommendation.tool.Api;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.util.List;

/**
 *
 * @description 针对表【agent(智能体)】的数据库操作Service实现
 * @Date 2024-05-19 11:07:38
 * @author Jasmine-drs
 */
@Service
public class AgentServiceImpl extends ServiceImpl<AgentMapper, Agent> implements AgentService{

	private final AgentMapper agentMapper;
	private final Api API;

	public AgentServiceImpl(AgentMapper agentMapper, Api api) {
		this.agentMapper = agentMapper;
		API = api;
	}

	@Override
	public List<Agent> getAgents(Integer studentAccount) {
		LambdaQueryWrapper<Agent> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Agent::getStudentAccount, studentAccount);
		return agentMapper.selectList(wrapper);
	}

	@Override
	public Agent addAgent(AgentAddDto addDto) throws MalformedURLException {
		Agent agent = API.getAgent(addDto.getDescription());
		agent.setImageUrl(addDto.getImageUrl());
		agent.setStudentAccount(addDto.getStudentAccount());
		agentMapper.insert(agent);
		LambdaQueryWrapper<Agent> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Agent::getStudentAccount, agent.getStudentAccount())
				.eq(Agent::getName, agent.getName())
				.eq(Agent::getDescription, agent.getDescription());
		return agentMapper.selectOne(wrapper);
	}
}




